import docx
import time
import os  # 引入os模块，用来创建文件夹
# 增加了参数folder表示文件夹
def invite(name, folder):
    template = 'template.docx'  
    name_placeholder = "<name>"  
    doc = docx.Document(template)
    for para in doc.paragraphs:
        if name_placeholder in para.text:
            for run in para.runs:
                if name_placeholder in run.text:
                    run.text = run.text.replace(name_placeholder, name)
    # 如果文件夹不存在，则自动创建文件夹
    if not os.path.exists(folder):
        os.makedirs(folder)
    # 保存的时候指定文件夹
    doc.save(f'{folder}/{name}.docx')

if __name__ == '__main__':
    # 调用的时候要传入文件夹，.代表当前文件夹
    invite('张三', ".")
    invite('李四', ".")
